package com.zlk.algorithm.dataStructure.tree.treeBase;

import com.zlk.algorithm.dataStructure.tree.TreeNode;
import org.junit.Test;

/**
 * @program: algorithm
 * @ClassName RecursiveTraversalBT
 * @description: 递归遍历二叉树
 * @author: slfang
 * @create: 2024-02-18 09:18
 * @Version 1.0
 **/
public class RecursiveTraversalBT {

    public void pre(TreeNode node){
        if(node==null){
            return ;
        }
        System.out.println(node.value);
        pre(node.left);
        pre(node.right);
    }

    public void mid(TreeNode node){
        if(node==null){
            return ;
        }
        mid(node.left);
        System.out.println(node.value);
        mid(node.right);
    }

    public void post(TreeNode node){
        if(node==null){
            return ;
        }
        post(node.left);
        post(node.right);
        System.out.println(node.value);
    }

    /**
     *
     *                                    1
     *                             2              3
     *                        4        5       6      7
     * 先序：1245367
     * 中序：4251637
     * 后续：4526731
     */
    @Test
    public void test(){
        TreeNode head = new TreeNode(1);
        head.left = new TreeNode(2);
        head.right = new TreeNode(3);
        head.left.left = new TreeNode(4);
        head.left.right = new TreeNode(5);
        head.right.left = new TreeNode(6);
        head.right.right = new TreeNode(7);

        pre(head);
        System.out.println("========");
        mid(head);
        System.out.println("========");
        post(head);
    }
}
